import { getAction, postAction, postFormAction3 } from '@/api/common'
import { deepClone } from '@/utils/common'
export default {
    data() {
        return {
            object: {},
            loading: true,
            dialogTableVisible: false,
            disabled: false,
            title: '',
            computedTitle: '',
            textMap: {
                show: '查看',
                edit: '编辑',
                add: '新增',
            },
            isReset: false,
        }
    },
    methods: {
        // 弹窗确认和取消方法
        confirm() {
            if (this.title == 'show') {
                this.dialogTableVisible = false
            } else {
                this.$refs.dataForm.validate((valid) => {
                    if (valid) {
                        this.loading = true
                        postAction(
                            this.title == 'add'
                                ? this.urlMap.create
                                : this.urlMap.edit,
                            this.handleFormDataToModel(this.object)
                        )
                            .then((res) => {
                                this.loading = false
                                if (res.flag) {
                                    this.$message.success(res.data)
                                    this.closeDialog(true)
                                    this.cancel()
                                } else {
                                    this.$message.error(res.data)
                                }
                            })
                            .catch((err) => {
                                this.$message.error('服务器错误，请稍后重试！')
                            })
                    }
                })
            }
        },
        cancel() {
            this.object = {}
            this.dialogTableVisible = false
            // this.$emit('reloadList');
        },
        closeDialog(isReload) {
            isReload ? this.$emit('reloadList') : ''
            // this.visible = false
        },
        showDialog(title, row) {
            this.title = title
            this.computedTitle = this.textMap[title]
            if (title == 'show') {
                // 查看的时候显示数据
                // this.object = row;
                this.handleModelToFormData(row)
                this.disabled = true
            } else if (title == 'add') {
                if (this.isReset) {
                    this.resetObj()
                } else {
                    this.object = {}
                }
                this.disabled = false
            } else {
                this.disabled = false
                // this.object = row;
                this.handleModelToFormData(row)
            }
            this.initData(title, row)
            this.dialogTableVisible = true
        },
        handleFormDataToModel(obj) {
            return obj
        },
        handleModelToFormData(data) {
            this.object = data ? deepClone(data) : {}
        },
        initData() {},
        resetObj() {},
    },
}
